package com.community.body.service.impl;


import com.community.body.domain.ZyBuilding;
import com.community.body.domain.ZyCommunity;
import com.community.body.domain.ZyUnit;
import com.community.body.mapper.ZyBuildingMapper;
import com.community.body.mapper.ZyCommunityMapper;
import com.community.body.mapper.ZyUnitMapper;
import com.community.body.service.IZyUnitService;
import com.community.body.util.LongUUIDGenerator;
import com.community.common.exception.ServiceException;
import com.community.common.utils.DateUtils;
import com.community.common.utils.StringUtils;
import com.community.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Validator;

import java.util.List;

/**
 * 单元 Service业务层处理
 * 
 * @author community
 * @date 2024-04-09
 */
@Service
public class ZyUnitServiceImpl implements IZyUnitService
{
    @Resource
    private ZyUnitMapper zyUnitMapper;
    @Resource
    private Validator validator;

    @Resource
    private ZyBuildingMapper buildingMapper;

    @Resource
    private ZyCommunityMapper communityMapper;


    /**
     * 查询单元 
     * 
     * @param unitId 单元 主键
     * @return 单元 
     */
    @Override
    public ZyUnit selectZyUnitByUnitId(Long unitId)
    {
        return zyUnitMapper.selectZyUnitByUnitId(unitId);
    }

    /**
     * 查询单元 列表
     * 
     * @param zyUnit 单元 
     * @return 单元 
     */
    @Override
    public List<ZyUnit> selectZyUnitList(ZyUnit zyUnit)
    {
        return zyUnitMapper.selectZyUnitList(zyUnit);
    }

    /**
     * 新增单元 
     * 
     * @param zyUnit 单元 
     * @return 结果
     */
    @Override
    public int insertZyUnit(ZyUnit zyUnit)
    {
        zyUnit.setCreateTime(DateUtils.getNowDate());
        String zyUnitCode = "Unit_";
        //时间戳
        long l = System.currentTimeMillis();
        zyUnit.setUnitCode(zyUnitCode + l );
        return zyUnitMapper.insertZyUnit(zyUnit);
    }

    /**
     * 修改单元 
     * 
     * @param zyUnit 单元 
     * @return 结果
     */
    @Override
    public int updateZyUnit(ZyUnit zyUnit)
    {
        zyUnit.setUpdateTime(DateUtils.getNowDate());
        return zyUnitMapper.updateZyUnit(zyUnit);
    }

    /**
     * 批量删除单元 
     * 
     * @param unitIds 需要删除的单元 主键
     * @return 结果
     */
    @Override
    public int deleteZyUnitByUnitIds(Long[] unitIds)
    {
        return zyUnitMapper.deleteZyUnitByUnitIds(unitIds);
    }

    /**
     * 删除单元 信息
     * 
     * @param unitId 单元 主键
     * @return 结果
     */
    @Override
    public int deleteZyUnitByUnitId(Long unitId)
    {
        return zyUnitMapper.deleteZyUnitByUnitId(unitId);
    }

    @Override
    public List<Object> selectZyList() {
        return zyUnitMapper.selectZyList();
    }

    @Override
    public String importUnit(List<ZyUnit> unitList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(unitList) || unitList.size() == 0) {
            throw new ServiceException("导入单元数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZyUnit unit : unitList) {
            try {
                // 验证是否存在这个单元
                ZyUnit u = zyUnitMapper.checkOne(unit.getUnitName());
                if (StringUtils.isNull(u) && checkBuilding(unit.getCommunityName(), unit.getBuildingName()) != null) {
                    BeanValidators.validateWithException(validator, unit);
                    ZyBuilding building = checkBuilding(unit.getCommunityName(), unit.getBuildingName());
                    unit.setBuildingId(building.getBuildingId());
                    unit.setCommunityId(building.getCommunityId());
                    unit.setUnitId(LongUUIDGenerator.generateLongUUID()[0]);
                    zyUnitMapper.insertZyUnit(unit);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、单元 " + unit.getUnitName() + " 导入成功");
                } else if (updateSupport) {
                    BeanValidators.validateWithException(validator, unit);
                    ZyBuilding building = checkBuilding(unit.getCommunityName(), unit.getBuildingName());
                    unit.setBuildingId(building.getBuildingId());
                    unit.setCommunityId(building.getCommunityId());
                    unit.setUnitId(u.getUnitId());
                    zyUnitMapper.updateZyUnit(unit);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、单元 " + unit.getUnitName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、单元 " + unit.getUnitName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、单元 " + unit.getUnitName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<ZyUnit> selectUnit(ZyUnit unit) {
        List<ZyUnit> unitList = zyUnitMapper.selectUnit(unit);
        return unitList;

    }

    @Override
    public List<ZyUnit> getAllUnit() {
        List<ZyUnit> unitList = zyUnitMapper.getAllUnit();
        return unitList;
    }

    @Override
    public List<ZyUnit> selectZyunitExport(Long[] longArray) {

       return zyUnitMapper.selectZyunitExport(longArray);
    }

    /**
     * 导入前的数据判断
     *
     * @return
     */
    public ZyBuilding checkBuilding(String communityName, String buildingName) {
        ZyCommunity community = communityMapper.checkCommunity(communityName);
        if (community == null) {
            return null;
        }
        ZyBuilding building = buildingMapper.checkBuilding(buildingName);
        if (building == null) {
            return null;
        }
        return building;
    }
}
